package com.example.lenjor.sprite.view;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.lenjor.sprite.R;
import com.example.lenjor.sprite.util.AnimUtil;

/**
 * Created by lenjor on 2017/7/1 0001.
 */
public class ShopView extends BaseView{
    private ImageView iv_chonghzi;
    private ImageView iv_goumai;
    private ImageView iv_kuangchong;
    private ImageView iv_buchongtili;
    private ImageView iv_buchongjingli;
    private FrameLayout fl_btns;

    private ImageView iv_fugure;
    private TranslateAnimation tranX;
    private TranslateAnimation tranY;
    private AnimationSet animSetFugure;
    private ChongZhiView chongZhiView;

    public ShopView(Context context){
        super(context);
        setContextView(R.layout.view_shop);
        initUI();
        fugureAnimation();
    }

    private void fugureAnimation() {
        animSetFugure = new AnimationSet(false);
        ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 1f, 1f, 0.96f,
                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1f);
        scaleAnimation.setDuration(900);
        scaleAnimation.setRepeatCount(Animation.INFINITE);  //设置重复次数为一直循环
        scaleAnimation.setRepeatMode(Animation.REVERSE);    //循环为反复
        //iv_fugure.startAnimation(scaleAnimation);       //启用动画
        animSetFugure.addAnimation(scaleAnimation);

        tranX = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
        tranX.setDuration(600);
        animSetFugure.addAnimation(tranX);

    }
    /**
     * 封装show方法，展示开始时调用
     */
    public void show(){
        tranY = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 0f);
        tranY.setDuration(600);
        fl_btns.startAnimation(tranY);

        tranY.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                iv_fugure.setVisibility(View.VISIBLE);
                iv_fugure.startAnimation(animSetFugure);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    public void hide(Animation.AnimationListener listener){
        tranY = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -1f);
        tranY.setDuration(300);
        fl_btns.startAnimation(tranY);
        tranX = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -1f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_PARENT, 0f);
        tranX.setDuration(300);
        iv_fugure.startAnimation(tranX);
        if(listener != null){
            tranX.setAnimationListener(listener);
        }
        iv_fugure.setVisibility(View.INVISIBLE);


    }

    @Override
    protected void initUI() {
        iv_chonghzi = (ImageView) findViewById(R.id.iv_chongzhi);
        AnimUtil.addOnTouchListener(iv_chonghzi, 0.5f, 0.5f, new AnimUtil.AnimListener() {
            @Override
            public void down(View v) {

            }

            @Override
            public void move(View v) {

            }

            @Override
            public void up(View v) {
                chongZhiView.show();
            }
        });
        iv_goumai = (ImageView) findViewById(R.id.iv_goumai);
        AnimUtil.addOnTouchListener(iv_goumai, 0.5f, 0.5f, null);
        iv_kuangchong = (ImageView) findViewById(R.id.iv_kuangchong);
        AnimUtil.addOnTouchListener(iv_kuangchong, 0.5f, 0.5f, null);
        iv_buchongtili = (ImageView) findViewById(R.id.iv_buchongtili);
        AnimUtil.addOnTouchListener(iv_buchongtili, 0.5f, 0.5f, null);
        iv_buchongjingli = (ImageView) findViewById(R.id.iv_buchongjingli);
        AnimUtil.addOnTouchListener(iv_buchongjingli, 0.5f, 0.5f, null);
        iv_fugure = (ImageView) findViewById(R.id.iv_fugure);
        iv_fugure.setVisibility(View.INVISIBLE);
        fl_btns = (FrameLayout) findViewById(R.id.fl_btns);

        chongZhiView = new ChongZhiView(context);
    }
}
